package net.wanho.systemSettings.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.systemSettings.dao.MenuDAO;
import net.wanho.systemSettings.po.Menu;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MenuDAOImpl extends BaseDAOImpl<Menu> implements MenuDAO {
    MenuDAO menuDAO = BeanFactory.getBean(MenuDAO.class);
    @Override
    public boolean insert(Menu menu) throws DAOException {
        try {
            String sql = "insert\n" +
                    " into\n" +
                    " menu (MENU_NAME,\n" +
                    " MENU_URL,\n" +
                    " PICTURES,\n" +
                    " STATUS,\n" +
                    " CREATE_TIME,\n" +
                    " UPDATE_TIME,\n" +
                    " PARENT_MENU_ID)\n" +
                    "values(?,?,?,?,now(),now(),?)";
            Object[] params = {menu.getMenuName(),menu.getMenuUrl(),menu.getPictures(),menu.getStatus(),menu.getParentMenuId()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean delete(String id) throws DAOException {
        try {
            String sql = "delete\n" +
                    "from\n" +
                    "menu\n" +
                    "where\n" +
                    "MENU_ID = ?";
            Object [] params = {id};
            return super.jdbcTemplate.delete(id);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean update(Menu menu) throws DAOException {
        try {
            String sql = "update\n" +
                    " menu\n" +
                    "set\n" +
                    " MENU_NAME = ?,\n" +
                    " MENU_URL = ?,\n" +
                    " PICTURES = ?,\n" +
                    " STATUS = ?,\n" +
                    " CREATE_TIME = now(),\n" +
                    " UPDATE_TIME = now(),\n" +
                    " PARENT_MENU_ID = ?\n" +
                    "where\n" +
                    " MENU_ID = ?";
            Object [] params = {menu.getMenuName(),menu.getMenuUrl(),menu.getPictures(),menu.getStatus(),
            menu.getParentMenuId(),menu.getMenuId()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Menu findById(String id) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  s.MENU_ID           menuId,\n" +
                    "  s.MENU_NAME         menuName,\n" +
                    "  s.MENU_URL          menuUrl,\n" +
                    "  s.PICTURES          pictures,\n" +
                    "  s.STATUS            STATUS,\n" +
                    "  s.CREATE_TIME       createTime,\n" +
                    "  s.UPDATE_TIME       updateTime,\n" +
                    "  s.PARENT_MENU_ID    parentMenuId\n" +
                    "FROM menu s\n" +
                    "where s.MENU_ID = ? ";
            Object [] params = {id};
            return super.queryForObject(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Menu> selectAll() throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  s.MENU_ID           menuId,\n" +
                    "  s.MENU_NAME         menuName,\n" +
                    "  s.MENU_URL          menuUrl,\n" +
                    "  s.PICTURES          pictures,\n" +
                    "  s.STATUS            STATUS,\n" +
                    "  s.CREATE_TIME       createTime,\n" +
                    "  s.UPDATE_TIME       updateTime,\n" +
                    "  s.PARENT_MENU_ID    parentMenuId\n" +
                    "FROM menu s";
            Object [] params = {};
            return super.queryForList(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Menu> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  s.MENU_ID           menuId,\n" +
                    "  s.MENU_NAME         menuName,\n" +
                    "  s.MENU_URL          menuUrl,\n" +
                    "  s.PICTURES          pictures,\n" +
                    "  s.STATUS            STATUS,\n" +
                    "  s.CREATE_TIME       createTime,\n" +
                    "  s.UPDATE_TIME       updateTime,\n" +
                    "  s.PARENT_MENU_ID    parentMenuId\n" +
                    "FROM menu s\n" +
                    "WHERE 1 = 1 ";
            //动态拼接SQL
            StringBuffer sb = new StringBuffer(sql);
            //条件无效  或者 没有条件
            boolean isCond = condMap == null || condMap.isEmpty();
            //创建集合 存放 动态参数值 ？ 替换的值
            List<Object> condParamValueList = new ArrayList<>();
            //有效条件
            if (!isCond && StringUtils.isNotEmpty(condMap.get("menuname"))) {
                sb.append(" AND MENU_NAME like ?") ;
                condParamValueList.add("%"+condMap.get("menuname")+"%") ;
            }
            Object[] params = condParamValueList.toArray();
            return super.queryForList(sb.toString(), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean updelete(Menu entity) throws DAOException {
        return super.updelete(entity);
    }

}
